package com.jishucai;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * 定义服务端启动类
 * @author admin
 */
public class SomeServer {
    public static void main(String[] args) {
        NioEventLoopGroup parentGroup = new NioEventLoopGroup();
        NioEventLoopGroup childGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup,childGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new LineBasedFrameDecoder(5120));
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new SomeServerHandler());
                        }
                    });

            ChannelFuture future = serverBootstrap.bind(8888).sync();
            future.channel().closeFuture().sync();


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }

    }




























//    NioEventLoopGroup parentGroup = new NioEventLoopGroup();
//    NioEventLoopGroup childGroup = new NioEventLoopGroup();
//
//        try {
//        ServerBootstrap bootstrap = new ServerBootstrap();
//
//        bootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class)
//                .childHandler(new ChannelInitializer<SocketChannel>() {
//
//                    @Override
//                    protected void initChannel(SocketChannel socketChannel) throws Exception {
//                        ChannelPipeline pipeline = socketChannel.pipeline();
//                        pipeline.addLast(new StringDecoder());
//                        pipeline.addLast(new StringEncoder());
//
//                        pipeline.addLast(new SomeServerHandler());
//                    }
//                });
//
//        ChannelFuture future = bootstrap.bind(8888).sync();
//        System.out.println("服务器已启动");
//        future.channel().closeFuture().sync();
//    } catch (InterruptedException e) {
//        e.printStackTrace();
//    } finally {
//        parentGroup.shutdownGracefully();
//        childGroup.shutdownGracefully();
//    }
}
